/*
 * Copyright (c) www.bugull.com
 */

package com.bugull.farm.app.server.processor;

import org.apache.logging.log4j.*;

import com.bugull.farm.core.frame.structure.Frame;
import com.bugull.farm.core.handler.Processor;
import com.bugull.farm.core.store.Device;
import com.bugull.farm.core.utils.ByteUtil;
import com.bugull.mongo.BuguDao;
import com.bugull.mongo.cache.DaoCache;

import io.netty.channel.ChannelHandlerContext;

/**
 * For command: CMD_APP_DEV_ONLINE
 *
 * @author Frank Wen(xbwen@hotmail.com)
 */
public class DeviceOnlineProcessor implements Processor {
	
	private final Logger logger = LogManager.getLogger(DeviceOnlineProcessor.class);
	
    @Override
    public void process(ChannelHandlerContext ctx, Frame frame) throws Exception{
        String mac = frame.getHexStr("mac");

        BuguDao<Device> dao = DaoCache.getInstance().get(Device.class);
        Device device = dao.findOne("macAddress", mac);

        byte result = (byte)0x00;
        if(device != null && device.isOnline()){
            result = 0x01;
        }
        frame.setByte("status", result);
        ctx.writeAndFlush(frame.getFastResponse());
    }

}
